import java.util.Random;
import java.util.Scanner;

// 定义CanPlay接口，包含攻击和防御方法
interface CanPlay {
    void attack(Actor a);
    void defense();
}

// 定义角色基类Actor，实现CanPlay接口
abstract class Actor implements CanPlay {
    String name; // 角色名称
    int blood; // 血量
    int state; // 状态
    int attackPower; // 攻击力
    int defensePower; // 防御力
    double criticalChance; // 暴击概率
    int criticalMultiplier; // 暴击倍数

    // 构造函数，初始化角色属性
    public Actor(String name, int blood, int attackPower, int defensePower, double criticalChance, int criticalMultiplier) {
        this.name = name;
        this.blood = blood;
        this.attackPower = attackPower;
        this.defensePower = defensePower;
        this.criticalChance = criticalChance;
        this.criticalMultiplier = criticalMultiplier;
    }

    // 抽象方法，子类必须实现
    public abstract void attack(Actor a);
    public abstract void defense();

    // 角色受到伤害
    public void takeDamage(int damage) {
        this.blood -= damage;
        if (this.blood < 0) {
            this.blood = 0;
        }
    }

    // 判断角色是否存活
    public boolean isAlive() {
        return this.blood > 0;
    }
}

// 定义Game基类，包含游戏的基本结构
abstract class Game {
    int numActors; // 角色数量
    Actor a1; // 角色1
    Actor a2; // 角色2
    boolean isAutoMode; // 是否为自动模式

    // 抽象方法，子类必须实现
    public abstract Actor randomActor(); // 随机生成角色
    public abstract void setActor(String type); // 根据用户输入设置角色

    // 游戏对战逻辑
    public void play() {
        for (int round = 1; round <= 7; round++) { // 默认 7 回合
            System.out.println("\n--- Round " + round + " ---");

            // 检查是否有一方已经死亡
            if (!a1.isAlive() || !a2.isAlive()) {
                break; // 结束游戏
            }

            if (!isAutoMode) {
                Scanner scanner = new Scanner(System.in);
                System.out.println("Choose to attack or defend (1 for attack, 2 for defend):");
                int actionChoice = scanner.nextInt();
                if (actionChoice == 1) {
                    a1.attack(a2); // 用户选择攻击
                } else if (actionChoice == 2) {
                    a1.defense(); // 用户选择防御
                } else {
                    System.out.println("Invalid choice, defaulting to attack");
                    a1.attack(a2);
                }
            } else {
                // 自动模式下随机选择攻击或防御
                Random rand = new Random();
                if (rand.nextBoolean()) {
                    a1.attack(a2);
                } else {
                    a1.defense();
                }
            }

            // 检查是否有一方已经死亡
            if (!a1.isAlive() || !a2.isAlive()) {
                break; // 结束游戏
            }

            // 对手自动选择攻击或防御
            Random rand = new Random();
            if (rand.nextBoolean()) {
                a2.attack(a1);
            } else {
                a2.defense();
            }

            // 输出当前回合结果
            System.out.println(a1.name + " blood: " + a1.blood + ", defense: " + a1.defensePower);
            System.out.println(a2.name + " blood: " + a2.blood + ", defense: " + a2.defensePower);
        }

        // 输出最终结果
        System.out.println("\n--- Game Over ---");
        System.out.println(a1.name + " final blood: " + a1.blood + ", defense: " + a1.defensePower);
        System.out.println(a2.name + " final blood: " + a2.blood + ", defense: " + a2.defensePower);
        if (!a1.isAlive()) {
            System.out.println(a2.name + " wins by defeating " + a1.name + "!");
        } else if (!a2.isAlive()) {
            System.out.println(a1.name + " wins by defeating " + a2.name + "!");
        } else if (a1.blood > a2.blood) {
            System.out.println(a1.name + " wins!");
        } else if (a2.blood > a1.blood) {
            System.out.println(a2.name + " wins!");
        } else {
            System.out.println("It's a tie!");
        }
    }
}

// 定义Game1类，继承Game基类
class Game1 extends Game {
    // 定义Master角色，继承Actor
    private class Master extends Actor {
        public Master(String name) {
            super(name, 100, 50, 5, 0.2, 2); // 初始化属性，调整防御值和暴击概率
        }

        @Override
        public void attack(Actor a) {
            Random rand = new Random();
            boolean isCritical = rand.nextDouble() < this.criticalChance;
            int damage = this.attackPower / a.defensePower;
            if (isCritical) {
                damage *= this.criticalMultiplier;
                System.out.println(this.name + " attacks " + a.name + " with a critical hit, dealing " + damage + " damage!");
            } else {
                System.out.println(this.name + " attacks " + a.name + " with normal attack, dealing " + damage + " damage!");
            }
            a.takeDamage(damage);
        }

        @Override
        public void defense() {
            this.defensePower += 1; // 防御值 +1
            System.out.println(this.name + " is defending! Defense increased to " + this.defensePower);
        }
    }

    // 定义Warrior角色，继承Actor
    private class Warrior extends Actor {
        public Warrior(String name) {
            super(name, 300, 60, 8, 0.3, 3); // 初始化属性，调整防御值和暴击概率
        }

        @Override
        public void attack(Actor a) {
            Random rand = new Random();
            boolean isCritical = rand.nextDouble() < this.criticalChance;
            int damage = this.attackPower / a.defensePower;
            if (isCritical) {
                damage *= this.criticalMultiplier;
                System.out.println(this.name + " attacks " + a.name + " with a critical hit, dealing " + damage + " damage!");
            } else {
                System.out.println(this.name + " attacks " + a.name + " with normal attack, dealing " + damage + " damage!");
            }
            a.takeDamage(damage);
        }

        @Override
        public void defense() {
            this.defensePower += 1; // 防御值 +1
            System.out.println(this.name + " is defending! Defense increased to " + this.defensePower);
        }
    }

    @Override
    public Actor randomActor() {
        Random rand = new Random();
        // 随机生成Master或Warrior角色
        if (rand.nextBoolean()) {
            return new Master("RandomMaster");
        } else {
            return new Warrior("RandomWarrior");
        }
    }

    @Override
    public void setActor(String type) {
        // 根据用户输入设置角色
        if (type.equalsIgnoreCase("Master")) {
            this.a1 = new Master("PlayerMaster");
        } else if (type.equalsIgnoreCase("Warrior")) {
            this.a1 = new Warrior("PlayerWarrior");
        } else {
            System.out.println("Invalid type, defaulting to Master");
            this.a1 = new Master("PlayerMaster");
        }
        this.a2 = randomActor(); // 随机生成对手角色
    }
}

// 定义Game2类，继承Game基类
class Game2 extends Game {
    // 定义Archer角色，继承Actor
    private class Archer extends Actor {
        public Archer(String name) {
            super(name, 150, 70, 6, 0.25, 2); // 初始化属性，调整防御值和暴击概率
        }

        @Override
        public void attack(Actor a) {
            Random rand = new Random();
            boolean isCritical = rand.nextDouble() < this.criticalChance;
            int damage = this.attackPower / a.defensePower;
            if (isCritical) {
                damage *= this.criticalMultiplier;
                System.out.println(this.name + " attacks " + a.name + " with a critical hit, dealing " + damage + " damage!");
            } else {
                System.out.println(this.name + " attacks " + a.name + " with normal attack, dealing " + damage + " damage!");
            }
            a.takeDamage(damage);
        }

        @Override
        public void defense() {
            this.defensePower += 1; // 防御值 +1
            System.out.println(this.name + " is defending! Defense increased to " + this.defensePower);
        }
    }

    // 定义Mage角色，继承Actor
    private class Mage extends Actor {
        public Mage(String name) {
            super(name, 80, 90, 4, 0.4, 3); // 初始化属性，调整防御值和暴击概率
        }

        @Override
        public void attack(Actor a) {
            Random rand = new Random();
            boolean isCritical = rand.nextDouble() < this.criticalChance;
            int damage = this.attackPower / a.defensePower;
            if (isCritical) {
                damage *= this.criticalMultiplier;
                System.out.println(this.name + " attacks " + a.name + " with a critical hit, dealing " + damage + " damage!");
            } else {
                System.out.println(this.name + " attacks " + a.name + " with normal attack, dealing " + damage + " damage!");
            }
            a.takeDamage(damage);
        }

        @Override
        public void defense() {
            this.defensePower += 1; // 防御值 +1
            System.out.println(this.name + " is defending! Defense increased to " + this.defensePower);
        }
    }

    @Override
    public Actor randomActor() {
        Random rand = new Random();
        // 随机生成Archer或Mage角色
        if (rand.nextBoolean()) {
            return new Archer("RandomArcher");
        } else {
            return new Mage("RandomMage");
        }
    }

    @Override
    public void setActor(String type) {
        // 根据用户输入设置角色
        if (type.equalsIgnoreCase("Archer")) {
            this.a1 = new Archer("PlayerArcher");
        } else if (type.equalsIgnoreCase("Mage")) {
            this.a1 = new Mage("PlayerMage");
        } else {
            System.out.println("Invalid type, defaulting to Archer");
            this.a1 = new Archer("PlayerArcher");
        }
        this.a2 = randomActor(); // 随机生成对手角色
    }
}

// 主类
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Choose a game (1 or 2):");
        int gameChoice = scanner.nextInt();
        scanner.nextLine(); // 清除换行符

        Game game;
        // 根据用户选择初始化游戏
        if (gameChoice == 1) {
            game = new Game1();
        } else if (gameChoice == 2) {
            game = new Game2();
        } else {
            System.out.println("Invalid choice, defaulting to Game 1");
            game = new Game1();
            gameChoice = 1;
        }

        System.out.println("Choose game mode (1 for auto, 2 for manual):");
        int modeChoice = scanner.nextInt();
        scanner.nextLine(); // 清除换行符
        game.isAutoMode = (modeChoice == 1); // 设置游戏模式

        if (gameChoice == 1) {
            System.out.println("Choose your character type (e.g., Master, Warrior):");
        } else if (gameChoice == 2) {
            System.out.println("Choose your character type (e.g., Archer, Mage):");
        }
        String characterType = scanner.nextLine();
        game.setActor(characterType); // 设置用户角色

        game.play(); // 开始游戏
        scanner.close();
    }
}